//related to picker3.ui
var page = sm("do_Page");
var app = sm("do_App");
var storage = sm("do_Storage");
var nf = sm("do_Notification");
var root = ui("$");
var urlUser = require("url/user");
var qqHttp = require("helper/http");

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(360,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:460};
animPanelShow.append(360,propsPS,"EaseOut");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(360,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(360,propsPH,"EaseIn");

//picker
var labtit = ui("do_Label_2");
var pickerbg = ui("pickerbg");//遮罩
var btnsure = ui("do_Button_1");//确定按钮
var pickerbox = ui("do_ALayout_1");
var delicacy = [];
var delicacyGrids = mm("do_ListData");
var do_GridView_delicacy = ui("do_GridView_delicacy");
qqHttp.post(urlUser.url.base.getUserBaseConfig, {type:3}, function(data) {
	for(var i=0;i<data.length;i++)
	{
	var obj={"val":data[i].id,"name":data[i].name};
	delicacy.push(obj)
	}
	delicacyGrids.addData(delicacy);
	do_GridView_delicacy.bindItems(delicacyGrids);
});

var userId;
var param= {};
var arraySelect;
var strSelect;

page.on("delicacy",function(data){
	pickerbg.visible = true;
	pickerbg.animate(animMaskShow);
	pickerbox.animate(animPanelShow);
})

//初始化
page.on("getDelicacy",function(data){
	strSelect="";
	arraySelect=[];
	param.id = data.id;
	var data_temp = delicacyGrids.getRange(0);
	for (var i = 0; i < data_temp.length; i++) {
		if(data.initSelect && data.initSelect.indexOf(data_temp[i].name) >= 0){
			delicacyGrids.updateOne(i, {"name":data_temp[i].name,"selected":1,"bgColor":"00000000"});	
		}
	}

	do_GridView_delicacy.refreshItems();

})

//确定事件
btnsure.on("touch",function(){
	/*if (!param) {
		nf.toast("请选择爱好");
		return;
	}*/
	
	//返回已选择的爱好
	var tmp = delicacyGrids.getRange(0);
	for(var i = 0; i < delicacyGrids.getCount(); i++){
		if(tmp[i].selected == "1"){
			arraySelect.push(tmp[i].name);
		}
	}
	
	if(arraySelect.length > 0){
		strSelect = arraySelect.join(",");
	}
	param.delicacys = strSelect;
	
	pickerbox.animate(animPanelHide);
	pickerbg.animate(animMaskHide,function(){
		pickerbg.visible = false;
	});
	
	qqHttp.post(urlUser.url.base.doUpdate, param, function(data) {
	});
	
	page.fire("refreshDelicacy",param);
});

//遮罩事件
pickerbg.on("touch","",300,function(){
	pickerbox.animate(animPanelHide);
	pickerbg.animate(animMaskHide,function(){
		pickerbg.visible = false;
	});
});

//防穿透
pickerbox.on("touch",function(){})

//订阅GridView的touch点击事件
do_GridView_delicacy.on("touch", function(data) {
	//nf.alert("第" + (data + 1) + "条数据被点中");
	var data_temp = delicacyGrids.getRange(0);
	if (data_temp[data].bgColor == 'C4C9D0FF') {
		return;
	}

	for (var i = 0; i < delicacyGrids.getCount(); i++) {
        if (i == data) {
    		if(data_temp[i].selected == "1"){
    			//取消
            	delicacyGrids.updateOne(i, {"name":data_temp[i].name,"selected":0,"bgColor":"00000000"});
    		}else{
    			//选中
            	delicacyGrids.updateOne(i, {"name":data_temp[i].name,"selected":1,"bgColor":"00000000"});
    		}	
        }
    }   
	
	do_GridView_delicacy.refreshItems();
	
})